package xyz.naoyu.dao;


import com.alibaba.fastjson.JSONObject;


import xyz.naoyu.settings;
import xyz.naoyu.utils.InfoPack;

import java.io.IOException;
import java.sql.*;

/**
 * @author yWieLoc
 * @date 2022/3/15 16:58
 */
public class BaseDao {
    Connection conn = null;

    /**
     * phoenix 连接
     */
    public Connection getConnection() {
        if(conn == null) {
            try {
                Class.forName(settings.driver);


                try {
                    conn = DriverManager.getConnection(settings.url);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                return conn;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                System.out.println("???????");
            }
        }
        return conn;
    }


    /**
     * 将sql结果集转为json格式
     * @param rowkey    关键值
     * @return 查询的结果(JSON)
     * @throws SQLException
     */

    public String getData(String type , String rowkey)  {
        if(conn == null)    getConnection();
        String sql = "select * from " + "\"" + type + "\"" + " where \"Id\" = ?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,rowkey);
            ResultSet res =  ps.executeQuery();
            return InfoPack.resToJSON(res);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

//        System.out.println( InfoPack.resToJSON(res) );

    }




    /**
     * 将json数据存入数据库中
     * @param type  请求类型/表
     * @param res   JSON对象
     * @return 成功提交的行数
     * @throws SQLException
     */

    public int putData(String type , JSONObject res) {
        if (conn == null)    getConnection();
        if (conn == null)   return -1;
        String sql = "upsert into " + "\"" + type + "\"" + InfoPack.toFill(  res.keySet(),"values" );
        System.out.println(sql);

        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            int i = 1;
            for(String a:res.keySet()) {
                ps.setString(i++,res.getString(a));
            }

            int cnt = ps.executeUpdate();
            conn.commit();
            System.out.println("增改信息行数: " + cnt);
            return cnt;
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }

        return 0;
    }

    /**
     * 删除指定关键字信息
     * @param type
     * @param rowkey
     * @return
     */
    public boolean delData(String type,String rowkey) {
        if(conn == null)    getConnection();
        type = "\""+ type + "\"";
        String sql = "delete from " + type + " where \"Id\" = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,rowkey);
            System.out.println("删除请求数 " + ps.executeUpdate() );
            conn.commit();
        } catch (SQLException throwables) {
            System.out.println("删除失败");
            throwables.printStackTrace();
            return false;
        }
        System.out.println("删除成功");
        return true;
    }

    /**
     *
     * @param type  查询表
     * @param options json 其中需要 direct order
     * @return  查询json
     */
    public String searchData(String type , JSONObject options) {
        if(conn == null)    getConnection();


        String sql = "select " + InfoPack.toList( options.getJSONObject("filter").keySet());
        sql += " from " + "\"" +  type + "\"";
        if(options.getJSONObject("direct") != null)
            sql += " where " + InfoPack.toFill(options.getJSONObject("direct").keySet(),options.getString("method"));
        if(options.getJSONObject("order") !=null)
            sql += " oder by " + InfoPack.toList(options.getJSONObject("order").keySet()) + " " + options.getString("xu");
        if(options.getIntValue("limit") != 0)
            sql += " limit " + options.getIntValue("limit");
        sql += " offset " +  (options.getIntValue("page")-1)*options.getIntValue("limit");


        System.out.println(sql);

        JSONObject direct = options.getJSONObject("direct");
        try {
            PreparedStatement ps = conn.prepareStatement(sql);

            if(direct != null) {
                int i = 1;
                for (String key : direct.keySet()) {
                    ps.setString(i++,direct.getString(key));
                }
            }


            ResultSet rs = ps.executeQuery();
            return InfoPack.resToJSON(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void clean() throws SQLException {
        if(conn == null) return;
        else {
            conn.close();
            conn = null;
        }
    }
}

